﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;

namespace WebUI.EditPages
{
    public partial class InvocieViewer : BasePage
    {
        int custID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if(Request.QueryString[CUSTID] != null && Request.QueryString[CUSTID] != "")
            {
                custID = int.Parse(Request.QueryString[CUSTID]);
            }

            if (!IsPostBack)
            {
                LoadInvoiceByCustID();
            }

            btnRedo.Enabled = Branches.CurrentBranchID == SessionUser.BranchID && SessionUser.Admin;
            if (Branches.CurrentBranchID != SessionUser.BranchID || !SessionUser.Admin)
                btnRedo.OnClientClick = "";
        }

        public void btnSearch_OnClick(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            View_Invoices ivc = null;
            if (custID > 0)
                ivc = View_Invoices.GetInvoiceByCustID(custID, txtSearch.Text);
            else
                ivc = View_Invoices.GetInvoices(txtSearch.Text);
            invoiceList.SourceObject = ivc;
            invoiceList.ReadOnly = true;
            RefreshList();
        }

        private void RefreshList()
        {
            invoiceList.CurrentPageIndex = 0;
            invoiceList.SortExpression = View_Invoices.ColumnNames.CreatedDate;
            invoiceList.SortDirection = BaseListControl.SORT_DESC;
            invoiceList.Filter = Invoices.ColumnNames.Deleted + "= 0";
            invoiceList.Refresh();
        }

        void LoadInvoiceByCustID()
        {
            if (custID > 0)
            {
                View_Invoices ivc = null;
                ivc = View_Invoices.GetInvoiceByCustID(custID);
                invoiceList.ReadOnly = true;
                invoiceList.SourceObject = ivc;

                RefreshList();
            }
        }

        protected void btnUnpaid_OnClick(object sender, EventArgs e)
        {
            LoadUnpaid();
        }

        private void LoadUnpaid()
        {
            View_Invoices ivc = null;
            ivc = View_Invoices.GetUnpaidInvoices(custID);
            invoiceList.SourceObject = ivc;
            invoiceList.ReadOnly = false;
            RefreshList();
        }

        protected void btnRedo_OnClick(object sender, EventArgs e)
        {
            try
            {
                if (hidSelectedID.Value == "")
                    return;

                int id = int.Parse(hidSelectedID.Value);

                Invoices.RevertInvoice(id, SessionUser.ID);
              
                Search();
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
            }
        }
    }
}
